﻿using Kenly.DBFramework.Gateway;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Kenly.UTAssist;

namespace Kenly.DBFramework.Tests
{


    /// <summary>
    ///这是 PagingResultTest 的测试类，旨在
    ///包含所有 PagingResultTest 单元测试
    ///</summary>
    [TestClass()]
    public class PagingResultTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试属性
        // 
        //编写测试时，还可使用以下属性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///PagingResult 构造函数 的测试
        ///</summary>
        [TestMethod()]
        public void PagingResultConstructorAbnormalTest()
        {
            AssertHelper helper = new AssertHelper(typeof(PagingResult), "PagingResult");

            PagingArg pagingArg = null;
            PagingResultConstructorAbnormalTest(helper, pagingArg);
        }

        private static void PagingResultConstructorAbnormalTest(AssertHelper helper, PagingArg pagingArg)
        {
            PagingResult target = new PagingResult(pagingArg);

            Assert.IsFalse(target.CanMoveFirst);
            Assert.IsFalse(target.CanMoveLast);
            Assert.IsFalse(target.CanMoveNext);
            Assert.IsFalse(target.CanMovePrevious);

            target = new PagingResult();
            Assert.IsFalse(target.CanMoveFirst, helper.BuildMessage(target.CanMoveFirst));
            Assert.IsFalse(target.CanMoveLast, helper.BuildMessage(target.CanMoveFirst));
            Assert.IsFalse(target.CanMoveNext, helper.BuildMessage(target.CanMoveFirst));
            Assert.IsFalse(target.CanMovePrevious, helper.BuildMessage(target.CanMoveFirst));
        }

        /// <summary>
        ///PagingResult 构造函数 的测试
        ///</summary>
        [TestMethod()]
        public void PagingResultConstructorTest()
        {
            //AssertHelper helper = new AssertHelper(typeof(PagingResult), "PagingResult");

            PagingArg pagingArg = new PagingArg(1, 20);
            pagingArg.PageCount = 10;
            pagingArg.RowCount = 200;

            PagingResult target = new PagingResult(pagingArg);

            Assert.IsTrue(target.CanMoveFirst);
            Assert.IsTrue(target.CanMoveLast);
            Assert.IsTrue(target.CanMoveNext);
            Assert.IsTrue(target.CanMovePrevious);

            Assert.IsTrue(target.RowCount == pagingArg.RowCount);
            Assert.IsTrue(target.PageCount == pagingArg.PageCount);

            Assert.IsTrue(target.PreviousPageIndex == pagingArg.PageIndex - 1);
            Assert.IsTrue(target.NextPageIndex == pagingArg.PageIndex + 1);
        }

        [TestMethod()]
        public void PagingResultConstructorLastPageTest()
        {
            PagingArg pagingArg = new PagingArg(1, 20);
            pagingArg.PageCount = 10;
            pagingArg.RowCount = 200;

            pagingArg.PageIndex = pagingArg.PageCount - 1;
            PagingResult target = new PagingResult(pagingArg);

            Assert.IsTrue(target.CanMoveFirst);
            Assert.IsFalse(target.CanMoveLast);
            Assert.IsFalse(target.CanMoveNext);
            Assert.IsTrue(target.CanMovePrevious);

            Assert.IsTrue(target.PreviousPageIndex == pagingArg.PageIndex - 1);
            Assert.IsTrue(target.NextPageIndex == pagingArg.PageIndex);
        }

        [TestMethod()]
        public void PagingResultConstructorFirstPageTest()
        {
            PagingArg pagingArg = new PagingArg(0, 20);
            pagingArg.PageCount = 10;
            pagingArg.RowCount = 200;

            PagingResult target = new PagingResult(pagingArg);

            Assert.IsFalse(target.CanMoveFirst);
            Assert.IsTrue(target.CanMoveLast);
            Assert.IsTrue(target.CanMoveNext);
            Assert.IsFalse(target.CanMovePrevious);

            Assert.IsTrue(target.PreviousPageIndex == pagingArg.PageIndex);
            Assert.IsTrue(target.NextPageIndex == pagingArg.PageIndex + 1);
        }

        [TestMethod()]
        public void PagingResultDefaultConstructorTest()
        {
            PagingResult target = new PagingResult { RowCount = 200, PageIndex = 5, PageSize = 20 };

            AssertAfterCalculating(target);
        }

        [TestMethod()]
        public void CalculateTest()
        {
            PagingResult target = new PagingResult { RowCount = 200, PageIndex = 5, PageSize = 20 };

            target.Calculate();
            AssertAfterCalculating(target);
        }

        private static void AssertAfterCalculating(PagingResult target)
        {
            Assert.IsTrue(target.CanMoveFirst);
            Assert.IsTrue(target.CanMoveLast);
            Assert.IsTrue(target.CanMoveNext);
            Assert.IsTrue(target.CanMovePrevious);

            Assert.IsTrue(target.PreviousPageIndex == target.PageIndex-1);
            Assert.IsTrue(target.NextPageIndex == target.PageIndex + 1);
        }

        [TestMethod()]
        public void ToStringTest()
        {
            PagingArg pagingArg = new PagingArg(0, 20);
            pagingArg.PageCount = 10;
            pagingArg.RowCount = 200;
            pagingArg.PageSize = 20;
            pagingArg.PageIndex = 2;

            PagingResult target = new PagingResult(pagingArg);
            string actual = target.ToString();

            Assert.IsTrue(actual.Contains("PageIndex:" + pagingArg.PageIndex));
            Assert.IsTrue(actual.Contains("RowCount:" + pagingArg.RowCount));
            Assert.IsTrue(actual.Contains("PageCount:" + pagingArg.PageCount));
            Assert.IsTrue(actual.Contains("PageSize:" + pagingArg.PageSize));
        }
    }
}
